package com.d0319;

import com.d0311.Vehiche;

public class EggTest {
    volatile boolean hasEggs = false;
    Object flag = new Object();

    Thread human = new Thread(()->{
       while (true) {
           synchronized (flag) {
               if (!hasEggs) {

                   try {
                       System.out.println("等待");
                       flag.wait();
                   } catch (InterruptedException e) {
                       e.printStackTrace();
                   }
               } else{
                   System.out.println("收取");
                   hasEggs = false;
               }
           }
       }
    });

    Thread human2 = new Thread(()->{
        while (true) {
            synchronized (flag) {
                if (!hasEggs) {

                    try {
                        System.out.println("等待1");
                        flag.wait();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                } else{
                    System.out.println("收取1");
                    hasEggs = false;
                }
            }
        }
    });

    Thread hen = new Thread(()->{
        while (true){
            try {
                Thread.sleep(2000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            hasEggs = true;
            synchronized (flag){
                flag.notify();
            }
        }
    });

    public static void main(String[] args) {
        EggTest eggTest = new EggTest();
        eggTest.hen.start();
        eggTest.human.start();
        eggTest.human2.start();
    }
}
